package com.victor.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.victor.controller.app.UserController;
import com.victor.entity.AdminToMenuOne;
import com.victor.entity.AdminToMenuTwo;
import com.victor.entity.Administrator;
import com.victor.entity.MenuOne;
import com.victor.entity.MenuTwo;
import com.victor.entity.step.Schedule;
import com.victor.entity.step.StepDetails;
import com.victor.service.AdministratorService;
import com.victor.utils.CodeUtil;
import com.victor.utils.Msg;
import com.victor.utils.wxlogin.SNSUserInfo;
import com.victor.utils.wxlogin.WxController;

@RestController
@RequestMapping("/management")
public class AdministratorCotroller {
	private static Logger log = LoggerFactory.getLogger(UserController.class);

	// 定义定时器
	private Timer timer = new Timer();

	// 定义验证码list
	private static List<Map<String, Object>> codelist = new ArrayList<Map<String, Object>>();

	@Autowired
	private AdministratorService administratorService;
	@Autowired
	private LogController logcontroller;

	/**
	 * 添加权限内容
	 * 
	 * @param onename
	 * @param twoname
	 * @param oneid
	 * @return
	 */
	@RequestMapping("/addpower")
	public Msg addPower(@RequestBody MenuOne menuOne) {
		
		String key = UUID.randomUUID().toString();
		
		if (menuOne.getAdminid() == null || menuOne.getAdminid().isEmpty()) {
			return Msg.fail("入参错误");
		}
		boolean b = administratorService.insertOne(menuOne);
		if (menuOne.getListtwo() != null || menuOne.getAdminid().isEmpty()) {
			boolean b2 = false;
			for (int i = 0; i < menuOne.getListtwo().size(); i++) {
				String menu_name = menuOne.getListtwo().get(i).getMenu_name();
				b2 = administratorService.insertTwo(new MenuTwo(null, menu_name, String.valueOf(menuOne.getId())));
			}
			if (!b2) {
				return Msg.fail();
			}
		}
		logcontroller.addLog(menuOne.getAdminid(), "添加了权限内容");
		return Msg.judge(b);
	}

	/**
	 * 添加二级权限内容
	 * 
	 * @param menuTwo
	 * @return
	 */
	@RequestMapping("/addtwopower")
	public Msg addTwoPower(MenuTwo menuTwo, String adminid) {
		if (menuTwo == null || menuTwo.getMenuone_id() == null || menuTwo.getMenuone_id().isEmpty()) {
			return Msg.fail("入参失败");
		}
		boolean b = administratorService.insertTwo(menuTwo);
		if (!b) {
			return Msg.fail("添加二级权限内容失败");
		}
		logcontroller.addLog(adminid, "添加了二级菜单内容");
		return Msg.success();

	}

	/**
	 * 修改一级菜单内容
	 * 
	 * @param menuOne
	 * @return
	 */
	@RequestMapping("/updone")
	public Msg upddateOne(MenuOne menuOne, String adminid) {
		if (menuOne.getId() == null || menuOne.getMenu_name().isEmpty() || adminid.isEmpty()) {
			return Msg.fail();
		}
		Boolean b = administratorService.updateOne(menuOne);
		logcontroller.addLog(adminid, "修改了权限一级菜单内容");
		return Msg.judge(b);
	}

	/**
	 * 修改二级菜单内容
	 * 
	 * @param menuOne
	 * @return
	 */
	@RequestMapping("/updtwo")
	public Msg upddateTwo(MenuTwo menuTwo, String adminid) {
		if (menuTwo.getId() == null || adminid.isEmpty()) {
			return Msg.fail("入参错误");
		}
		Boolean b = administratorService.updateTwo(menuTwo);
		logcontroller.addLog(adminid, "修改了权限二级菜单内容");
		return Msg.judge(b);
	}

	/**
	 * 批量删除 删除一级菜单
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/delone")
	public Msg deleteOne(int[] id, String adminid) {
		if (id == null || id.length == 0 || adminid == null || adminid.isEmpty()) {
			return Msg.fail("入参错误");
		}
		boolean b = administratorService.deleteOne(id);
		logcontroller.addLog(adminid, "删除了权限一级菜单");
		return Msg.judge(b);

	}

	/**
	 * 删除二级菜单
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/deltwo")
	public Msg deteleTwo(int[] id, String adminid) {
		if (id == null || id.length == 0 || adminid == null || adminid.isEmpty()) {
			return Msg.fail("入参错误");
		}
		boolean b = administratorService.deleteTwo(id);
		logcontroller.addLog(adminid, "删除了二级菜单");
		return Msg.judge(b);

	}

	/**
	 * 查询菜单
	 * 
	 * @param page
	 * @param row
	 * @return
	 */
	@RequestMapping("/menu")
	public Msg findMenu(int page, int row, String adminid) {
		if (adminid == null || adminid.isEmpty()) {
			return Msg.fail("入参错误");
		}
		PageInfo<MenuOne> menu = administratorService.getMenu(page, row);
		logcontroller.addLog(adminid, "查看了权限列表");
		return Msg.success().add("menulist", menu);
	}

	/**
	 * 添加管理员
	 * 
	 * @param administrator
	 * @return
	 */
	@RequestMapping("/addadmin")
	public Msg addAdmin(@RequestBody Administrator administrator) {
		if (administrator == null) {
			return Msg.fail("admin空");
		}
		boolean b = administratorService.insert(administrator);
		if (administrator.getAdministratorToMenuOnes() != null
				&& administrator.getAdministratorToMenuOnes().size() > 0) {
			for (int i = 0; i < administrator.getAdministratorToMenuOnes().size(); i++) {
				AdminToMenuOne adminToMenuOne = new AdminToMenuOne();
				adminToMenuOne.setMenuone_id(administrator.getAdministratorToMenuOnes().get(i).getMenuone_id());
				adminToMenuOne.setAdmin_id(String.valueOf(administrator.getId()));
				boolean b1 = administratorService.insertAdminMenuOne(adminToMenuOne);
				if (!b1) {
					return Msg.fail("管理员一级添加失败");
				}
				if (administrator.getAdministratorToMenuOnes().get(i).getAdministratorToMenuTwos() != null
						&& administrator.getAdministratorToMenuOnes().get(i).getAdministratorToMenuTwos().size() > 0) {
					for (int j = 0; j < administrator.getAdministratorToMenuOnes().get(i).getAdministratorToMenuTwos()
							.size(); j++) {
						AdminToMenuTwo adminToMenuTwo = new AdminToMenuTwo();
						adminToMenuTwo.setMenutwo_id(String.valueOf(administrator.getAdministratorToMenuOnes().get(i)
								.getAdministratorToMenuTwos().get(j).getMenutwo_id()));
						adminToMenuTwo.setMenuoneid(
								String.valueOf(administrator.getAdministratorToMenuOnes().get(i).getMenuone_id()));
						adminToMenuTwo.setAdmin_id(String.valueOf(administrator.getId()));
						boolean b2 = administratorService.insertAdminMenuTwo(adminToMenuTwo);
						if (!b2) {
							return Msg.fail("管理员二级添加失败");
						}
					}
				}

			}
		}
		logcontroller.addLog(administrator.getAdminid(), "添加了管理员");
		return Msg.judge(b);
	}

	/**
	 * 管理员添加权限
	 * 
	 * @param adminone
	 * @return
	 */
	@RequestMapping("/adminaddpower")
	public Msg addAdminPower(@RequestBody List<AdminToMenuOne> adminone) {
		if (adminone == null || adminone.get(0).getAdmin_id() == null || adminone.get(0).getAdmin_id().isEmpty()
				|| adminone.get(0).getAdminid() == null || adminone.get(0).getAdminid().isEmpty()) {
			return Msg.fail("入参错误");
		}
		List<AdminToMenuOne> aOne = administratorService.getByAdminId(adminone.get(0).getAdmin_id());
		if (aOne != null && aOne.size() > 0) {
			boolean b = administratorService.deleteMenuOneTwo(adminone.get(0).getAdmin_id());
			if (!b) {
				return Msg.fail("删除权限失败");
			}
		}
		for (int i = 0; i < adminone.size(); i++) {
			AdminToMenuOne adminToMenuOne = new AdminToMenuOne();
			adminToMenuOne.setMenuone_id(adminone.get(i).getMenuone_id());
			adminToMenuOne.setAdmin_id(adminone.get(i).getAdmin_id());
			administratorService.insertAdminMenuOne(adminToMenuOne);
			if (adminone.get(i).getAdministratorToMenuTwos() != null
					&& adminone.get(i).getAdministratorToMenuTwos().size() > 0) {
				for (int j = 0; j < adminone.get(i).getAdministratorToMenuTwos().size(); j++) {
					AdminToMenuTwo adminToMenuTwo = new AdminToMenuTwo();
					adminToMenuTwo.setMenutwo_id(adminone.get(i).getAdministratorToMenuTwos().get(j).getMenutwo_id());
					adminToMenuTwo.setMenuoneid(adminone.get(i).getMenuone_id());
					adminToMenuTwo.setAdmin_id(adminone.get(i).getAdmin_id());
					boolean b1 = administratorService.insertAdminMenuTwo(adminToMenuTwo);
					if (!b1) {
						return Msg.fail("管理员添加二级菜单失败");
					}
				}
			}
		}
		logcontroller.addLog(adminone.get(0).getAdminid(), "添加了" + adminone.get(0).getAdmin_id() + "的管理员权限");
		return Msg.success();
	}

	/**
	 * 添加管理员二级菜单权限
	 * 
	 * @param list
	 * @return
	 */
	@RequestMapping("/addadmintwomenu")
	public Msg addAdminTwoPower(@RequestBody List<AdminToMenuTwo> list) {
		if (list == null || list.get(0).getAdmin_id() == null || list.get(0).getAdmin_id().isEmpty()
				|| list.get(0).getMenutwo_id() == null || list.get(0).getMenutwo_id().isEmpty()
				|| list.get(0).getMenuoneid() == null || list.get(0).getMenuoneid().isEmpty()) {
			return Msg.fail("入参错误");
		}
		boolean b = administratorService.delMenuTwo(list.get(0).getAdmin_id(), list.get(0).getMenuoneid());
		if (!b) {
			return Msg.fail("删除二级菜单错误");
		}
		for (int i = 0; i < list.size(); i++) {
			boolean adminMenuTwo = administratorService.insertAdminMenuTwo(list.get(i));
			if (!adminMenuTwo) {
				return Msg.fail("插入二级菜单失败");
			}
		}
		logcontroller.addLog(list.get(0).getAdminid(), "修改了管理员" + list.get(0).getAdmin_id() + "的二级权限菜单");
		return Msg.success();

	}

	/**
	 * 修改管理员权限
	 * 
	 * @param administrator
	 * @return
	 */
	@RequestMapping("/updadminpower")
	public Msg updAdminPower(@RequestBody Administrator administrator) {
		if (administrator == null || administrator.getId() == null || administrator.getAdminid() == null
				|| administrator.getAdminid().isEmpty()) {
			return Msg.fail("管理员id为空");
		}
		if (administrator.getAdministratorToMenuOnes() != null
				&& administrator.getAdministratorToMenuOnes().size() > 0) {
			boolean b = administratorService.updateAdministrator(administrator);
			if (!b) {
				return Msg.fail("修改管理员失败");
			}
			List<AdminToMenuOne> aOne = administratorService.getByAdminId(String.valueOf(administrator.getId()));
			if (aOne != null && aOne.size() > 0) {
				boolean b1 = administratorService.deleteMenuOneTwo(String.valueOf(administrator.getId()));
				if (!b1) {
					return Msg.fail("删除权限失败");
				}
			}
			for (int i = 0; i < administrator.getAdministratorToMenuOnes().size(); i++) {
				AdminToMenuOne adminToMenuOne = new AdminToMenuOne();
				adminToMenuOne.setMenuone_id(administrator.getAdministratorToMenuOnes().get(i).getMenuone_id());
				adminToMenuOne.setAdmin_id(String.valueOf(administrator.getId()));
				boolean adminMenuOne = administratorService.insertAdminMenuOne(adminToMenuOne);
				if (!adminMenuOne) {
					return Msg.fail("修改一级菜单失败");
				}
				if (administrator.getAdministratorToMenuOnes().get(i).getAdministratorToMenuTwos() != null
						&& administrator.getAdministratorToMenuOnes().get(i).getAdministratorToMenuTwos().size() > 0) {
					for (int j = 0; j < administrator.getAdministratorToMenuOnes().get(i).getAdministratorToMenuTwos()
							.size(); j++) {
						AdminToMenuTwo adminToMenuTwo = new AdminToMenuTwo();
						adminToMenuTwo.setMenutwo_id(administrator.getAdministratorToMenuOnes().get(i)
								.getAdministratorToMenuTwos().get(j).getMenutwo_id());
						adminToMenuTwo.setMenuoneid(administrator.getAdministratorToMenuOnes().get(i).getMenuone_id());
						adminToMenuTwo.setAdmin_id(String.valueOf(administrator.getId()));
						boolean b1 = administratorService.insertAdminMenuTwo(adminToMenuTwo);
						if (!b1) {
							return Msg.fail("修改管理员二级菜单失败");
						}
					}
				}
			}
		}
		logcontroller.addLog(administrator.getAdminid(), "修改了" + administrator.getId() + "的管理员权限");
		return Msg.success();

	}

	/**
	 * 删除管理员
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("/deleteadmin")
	public Msg delteAdministrator(int[] ids, String adminid) {
		if (ids == null || ids.length <= 0 || adminid == null || adminid.isEmpty()) {
			return Msg.fail("入参错误");
		}
		boolean b = administratorService.delAdministrator(ids);
		logcontroller.addLog(adminid, "删除了管理员");
		if (b) {
			for (int id : ids) {
				// 查询审批流程是否用到此员工
				List<StepDetails> list = administratorService.selectStepDetails(id+"");
				// 如果用到将此员工流程删除，并将其后流程全部向前挪动一位
				if (list != null && list.size()!= 0) {
					for (int i = 0; i < list.size();i++) {
						//删除流程详细表
						boolean del = administratorService.delstepDetails(list.get(i).getSd_id());
						if (!del) {
							return Msg.fail("删除流程详细失败");
						}
						//查询此模板的所有详细表
						List<StepDetails> s_idList = administratorService.selectStepDetailsByS_id(list.get(i).getS_id(),list.get(i).getPosition());
						for (int j = 0; j < s_idList.size(); j++) {
							boolean update = administratorService.updatePosition(s_idList.get(j).getSd_id(),(s_idList.get(j).getPosition())-1);
							if (!update) {
								return Msg.fail("修改其后流程向前挪动一位失败");
							}
						}
					}
				}
				//查询审批进度中是否用到此员工
				List<Schedule> schedulesList = administratorService.selectSchedules(id+"");
				//如果审批进度中用到此员工
				if (schedulesList != null && schedulesList.size()!= 0) {
					for (int i = 0; i < schedulesList.size();i++) {
						//删除审核进度
						boolean del = administratorService.delSchedules(schedulesList.get(i).getId());
						if (!del) {
							return Msg.fail("删除审核进度失败");
						}
						//查询此模板排在此员工以后的审核进度
						List<Schedule> schedules = administratorService.selectScheduleByS_id(schedulesList.get(i).getS_id(),Integer.parseInt(schedulesList.get(i).getSp_sequence()));
						for (int j = 0; j < schedules.size(); j++) {
							//如果要删除的员工审核进度是（2.待我审批）
							if (schedulesList.get(i).getPersonal_status().equals("2") && j==0) {
								//将下一个员工状态改为 （2.待我审批）
								Schedule schedule = new Schedule();
								schedule.setId(schedules.get(j).getId());
								schedule.setPersonal_status("2");
								boolean s = administratorService.updateScheduleById(schedule);
								if (!s) {
									return Msg.fail("修改下一员工状态为2.带我审批失败");
								}
							}
							Schedule schedule = new Schedule();
							schedule.setId(schedules.get(j).getId());
							//修改审核进度向前挪动一位
							schedule.setSp_sequence(Integer.parseInt(schedules.get(j).getSp_sequence())-1+"");
							//如果要删除的员工审核人就是派单人，将派单人设置成下一个审批人
							if (schedulesList.get(i).getPd_empid().equals(id+"")) {
								schedule.setPd_empid(schedules.get(j).getSp_empid());
							}
							boolean updateSequence = administratorService.updateScheduleById(schedule);
							if (!updateSequence) {
								return Msg.fail("修改审核进度向前挪动一位失败");
							}
						}
					}
				}
			}
			return Msg.success();
		}
		return Msg.fail("删除管理员失败");

	}

	/**
	 * 根据管理员id查详情
	 * 
	 * @param administrator
	 * @return
	 */
	@RequestMapping("/xiangqing")
	public Msg findAdminById(Administrator administrator) {
		administrator = administratorService.getAdminById(administrator);
		logcontroller.addLog(String.valueOf(administrator.getId()), "查看了" + administrator.getId() + "详情");
		return Msg.success().add("administrator", administrator);

	}

	/**
	 * 管理员列表
	 * 
	 * @param page
	 * @param row
	 * @return
	 */
	@RequestMapping("/getall")
	public Msg findALLAdmin(int page, int row, String adminid) {
		if (adminid == null || adminid.isEmpty()) {
			return Msg.fail("入参失败");
		}
		PageInfo<Administrator> pageInfo = administratorService.getAllAdmin(page, row);
		logcontroller.addLog(adminid, "查看了管理员列表");
		return Msg.success().add("pageInfo", pageInfo);

	}
//	/**
//	 * 导出管理员
//	 * @param request
//	 * @param response
//	 * @throws Exception
//	 */
//	@RequestMapping("/export")
//	public void export(HttpServletRequest request,HttpServletResponse response,String adminid) throws Exception {
//		List<Administrator> list = administratorService.getAll();
//		SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();
//		Sheet sheet = sxssfWorkbook.createSheet("管理员列表");
//		CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 1, (short)0, (short)6);
//		sheet.addMergedRegion(cellRangeAddress);
//		Row headTitle = sheet.createRow(0);
//		headTitle.createCell(0).setCellValue("管理员列表");
//		
//		Row headrow = sheet.createRow(4);
//		headrow.createCell(0).setCellValue("管理员编号");
//		headrow.createCell(1).setCellValue("管理员姓名");
//		headrow.createCell(2).setCellValue("管理员部门");
//		headrow.createCell(3).setCellValue("管理员标签");
//		headrow.createCell(4).setCellValue("管理员照片");
//		headrow.createCell(5).setCellValue("账号");
//		headrow.createCell(6).setCellValue("密码");
//		
//		for(Administrator administrator : list) {
//			Row dataRow = sheet.createRow(sheet.getLastRowNum()+1);
//			if(administrator.getA_adminnumber() == null) {
//				dataRow.createCell(0).setCellValue("");
//			}else {
//				dataRow.createCell(0).setCellValue(administrator.getA_adminnumber());
//			}
//			
//			if(administrator.getA_adminname() == null) {
//				dataRow.createCell(1).setCellValue("");
//			}else {
//				dataRow.createCell(1).setCellValue(administrator.getA_adminname());
//			}
//			if(administrator.getA_department() == null) {
//				dataRow.createCell(2).setCellValue("");
//			}else {
//				dataRow.createCell(2).setCellValue(administrator.getA_department());
//			}
//			if(administrator.getA_label()== null) {
//				dataRow.createCell(3).setCellValue("");
//			}else {
//				dataRow.createCell(3).setCellValue(administrator.getA_label());
//			}
//			if(administrator.getA_photo() == null) {
//				dataRow.createCell(4).setCellValue("");
//			}else {
//				dataRow.createCell(4).setCellValue(administrator.getA_photo());
//			}
//			if(administrator.getA_username() == null) {
//				dataRow.createCell(5).setCellValue("");
//			}else {
//				dataRow.createCell(5).setCellValue(administrator.getA_username());
//			}
//			if(administrator.getA_password() == null) {
//				dataRow.createCell(6).setCellValue("");
//			}else {
//				dataRow.createCell(6).setCellValue(administrator.getA_password());
//			}
//		}
//		String filename = "管理员列表";
//		response.setCharacterEncoding("UTF-8");
//		response.setContentType("application/vnd.ms-excel");
//		response.setHeader("Content-Disposition",
//				"attachment;filename=" + URLEncoder.encode(filename + ".xlsx", "UTF-8"));
//		ServletOutputStream outputStream = response.getOutputStream();
//		sxssfWorkbook.write(outputStream);
//		outputStream.close();
//		sxssfWorkbook.close();
//		logcontroller.addLog(adminid, "导出了管理员列表");
//	}

	/**
	 * . 搜索
	 * 
	 * @param search
	 * @return
	 */
	@RequestMapping("/search")
	public Msg search(String search, String adminid) {
		if (search == null || search.isEmpty() || adminid == null || adminid.isEmpty()) {
			return Msg.fail("入参错误");
		}
		List<Administrator> list = administratorService.selBySearch(search);
		logcontroller.addLog(adminid, "根据" + search + "对管理员列表进行搜索");
		return Msg.success().add("list", list);
	}

	/**
	 * 根据时间搜索
	 * 
	 * @param start
	 * @param end
	 * @param page
	 * @param row
	 * @return
	 */
	@RequestMapping("/searchtime")
	public Msg searchTime(String start, String end, int page, int row) {
		if (start == null && end == null) {
			return Msg.fail("入参错误");
		}
		PageInfo<Administrator> list = administratorService.selByTime(start, end, page, row);
		return Msg.success().add("list", list);

	}

	/**
	 * 微信登陆
	 * 
	 * @param code
	 * @return
	 */
	@PostMapping("/wechartLogin")
	public Msg wechartLogin(String code) {
		SimpleDateFormat sdf = new SimpleDateFormat();
		if (null == code || code.isEmpty()) {
			return Msg.fail();
		}
		SNSUserInfo snsUserInfo = null;
		try {
			snsUserInfo = WxController.weixinLogin(code);
			Administrator administrator = administratorService.findByOpenid(snsUserInfo.getOpenId());
			if (administrator == null) {
				administrator = new Administrator();
//				administrator.setOpenid(snsUserInfo.getOpenId());
//				StringBuffer sb = new StringBuffer();
//				sb.append(snsUserInfo.getCountry()==null?"":snsUserInfo.getCountry());
//				sb.append(snsUserInfo.getProvince()==null?"":snsUserInfo.getProvince());
//				sb.append(snsUserInfo.getCity()==null?"":snsUserInfo.getCity());
//				String address = sb.toString();
//				administrator.setAddress(address);
//				administrator.setHeadphoto(snsUserInfo.getHeadImgUrl());
				administrator.setInstime(sdf.format(new Date()));
				boolean b = administratorService.insert(administrator);
				if (!b) {
					return Msg.fail("创建管理员失败");
				}
			}
			return Msg.success().add("administrator", administrator);

		} catch (Exception e) {
			log.error("--UserController.wechartLogin->:{}", e);
			return Msg.fail();
		}

	}

	/**
	 * 发送验证码
	 * 
	 * @param phone
	 * @return
	 */
	@RequestMapping("/sendcode")
	public Msg validationPhone(String phone) {
		if (phone == null || phone.equals("")) {
			return Msg.fail("手机号为空");
		}
		// 关闭timer
		timer.cancel();
		// new一个Timer
		timer = new Timer();
		Map<String, Object> m2 = new HashMap<String, Object>();
		// 生成验证码(6位随机的验证码)
		String random = String.valueOf((int) ((Math.random() * 9 + 1) * 100000));
		System.out.println(phone + " ~~~" + random);
		// 第三方发送短息 
		// AliMobileCode.getMessage(mobile, random);
		// 删除该手机号在codelist中其他的验证信息
		CodeUtil.clearothermap(codelist, phone);
		// 将随机验证码放入到全局map中
		m2.put("random", random);
		m2.put("mobile", phone);
		codelist.add(m2);
		m2 = new HashMap<String, Object>();

		// 设置定时器2分钟清空mmmlist
		timer.schedule(new TimerTask() {
			public void run() {
				codelist.clear();
			}
		}, 2 * 60 * 1000);// 设定指定的时间time,此处为2*60*1000毫秒
		return Msg.success().add("code", random);
	}

	/**
	 * 短信登陆
	 * 
	 * @param phone
	 * @param code
	 * @return
	 */
	@RequestMapping("codelogin")
	public Msg codeLogin(String phone, String code) {
		if (phone == null || phone.isEmpty() || code == null || code.isEmpty()) {
			return Msg.fail("入参错误");
		}
		// 获取验证码
		String random = CodeUtil.getmapvalue(codelist, phone);
		if (code.equals(random)) {
			// 验证码用后失效..
			Map<String, String> map = new HashMap<String, String>();
			map.put("mobile", phone);
			map.put("random", random);
			codelist.remove(map);
			Administrator administrator = administratorService.selByPhone(phone);
			if (administrator == null) {
				return Msg.fail("该手机号不存在");
			}
			return Msg.success().add("administrator", administrator);
		} else {
			return Msg.fail("验证码错误");
		}
	}
}
